package com.zkingsoft.web.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.web.bean.WebAd;
import com.zkingsoft.web.service.WebAdService;
import com.zkingsoft.web.service.WebAdcolumnService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-12-20 15:50
 */
@Controller
@RequestMapping(value = "admin/webAd")
public class WebAdController {

	@Resource
	private WebAdService webAdService;

	@Resource
	private WebAdcolumnService webAdcolumnService;


	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebAd webAd, PaginationVO pageVo) {

		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webAd.setCompanyId(user.getCompanyId());
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS,  
				webAdService.findInPage(webAd, pageVo), webAdService.findTotal(webAd));
		return result;

	}

	/**
	 * 新增或者修改页面
	 */
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebAd webAd) {
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		if (webAd.getAdId() != null) {
			if(StringUtils.isBlank(webAd.getAdTitle())){
				webAd.setAdTitle("　");
			}
			webAd.setCompanyId(user.getCompanyId());
			int i = webAdService.modifyByModel(webAd);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "内容修改成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL,  "内容修改失败");
			}

		} else {
			if (webAd.getAdTime() == null) {
				webAd.setAdTime(new Date());
			}
			
			webAd.setCompanyId(user.getCompanyId());
			int i = webAdService.add(webAd);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "内容新增成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL,  "内容新增失败");
			}
		}
	}

	/**
	 * 进入修改界面
	 */
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		WebAd webAd;
		if (id != null) {
			webAd = webAdService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webAd);
		}
		return "admin/web/ad-form";
	}

	/**
	 * 删除
 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = webAdService.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL,  "删除失败");
		}
	}
	
}